define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'linestation/index' + location.search,
                    add_url: 'linestation/add/line_id/' + Config.ids,
                    edit_url: 'linestation/edit',
                    del_url: 'linestation/del',
                    multi_url: 'linestation/multi',
                    import_url: 'linestation/import',
                    table: 'line_station',
                }
            });

            var table = $("#table");

            console.log(Config.ids);

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                // fixedColumns: false,
                fixedRightNumber: 1,
                showExport:false,//去掉导出
                // search:false,//去掉ID搜索
                // commonSearch:false,//去掉放大镜
                // showToggle:false,//去掉切换
                // showColumns:false,//去掉列
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'line_id', title: __('Line_id')},
                        // {field: 'station_id', title: __('Station_id')},
                        {field: 'area', title: __('Area'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'station_name', title: __('Station_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'coordinate_lat', title: __('Coordinate_lat'), operate: 'LIKE'},
                        // {field: 'coordinate_lng', title: __('Coordinate_lng'), operate: 'LIKE'},
                        {field: 'is_shang', title: __('Is_shang'), searchList: {"0":__('Is_shang 0'),"1":__('Is_shang 1')}, formatter: Table.api.formatter.normal},
                        {field: 'is_jie', title: __('Is_jie'), searchList: {"0":__('Is_jie 0'),"1":__('Is_jie 1')}, formatter: Table.api.formatter.normal},
                        {field: 'is_xia', title: __('Is_xia'), searchList: {"0":__('Is_xia 0'),"1":__('Is_xia 1')}, formatter: Table.api.formatter.normal},
                        {field: 'is_song', title: __('Is_song'), searchList: {"0":__('Is_song 0'),"1":__('Is_song 1')}, formatter: Table.api.formatter.normal},
                        {field: 'switch', title: __('Switch'), searchList: {"0":__('Switch 0'),"1":__('Switch 1')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'tags', title: __('Tags'), operate: 'LIKE', formatter: Table.api.formatter.flag},
                        {field: 'map_type', title: __('Map_type'), searchList: {"1":__('Map_type 1'),"2":__('Map_type 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'map_json', title: __('Map_json')},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        // {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams: function (params) {
                    //如果希望追加搜索条件,可使用
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    filter.line_id = Config.ids;
                    op.line_id = '=';
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);

                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify({player_id: Config.ids});
                    //params.op = JSON.stringify({player_id: '='});
                    return params;
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();

            $("[data-toggle='wleaflet-select']").data("callback", function(res){
                var selectedValue =$("input[name='row[map_type]']:checked").val();

                if(selectedValue == 2){
                    res = JSON.stringify(res);
                    var now_val = $("#c-map_json").val();
                    var latlng = now_val ? ',' + res : res;
                    now_val += latlng;
                }else{
                    var now_val = res.latlng;
                }
                
                $("#c-map_json").val(now_val);
            });

            $("input[name='row[map_type]']").change(function(){
                $("#c-map_json").val('');
            });
        
        },
        edit: function () {
            Controller.api.bindevent();

            var selectedValue =$("input[name='row[map_type]']:checked").val();
            var now_val = $("#c-map_json").val();
            if(selectedValue == 2){
                now_val = now_val.split("[").join("");
                now_val = now_val.split("]").join("");
                $("#c-map_json").val(now_val);
            }

            $("[data-toggle='wleaflet-select']").data("callback", function(res){
                if(selectedValue == 2){
                    res = JSON.stringify(res);
                    var now_val = $("#c-map_json").val();
                    var latlng = now_val ? ',' + res : res;
                    now_val += latlng;
                }else{
                    var now_val = res.latlng;
                }
                
                $("#c-map_json").val(now_val);
            });

            $("input[name='row[map_type]']").change(function(){
                $("#c-map_json").val('');
            });

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
